package com.biz.primus.ms.homepage.dao.ro;


import com.biz.primus.base.enums.CommonStatus;
import com.biz.primus.base.redis.annotation.Ro;
import com.biz.primus.base.redis.annotation.RoSortedSet;
import com.biz.primus.base.redis.bean.BaseRedisObject;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.apache.commons.collections.ComparatorUtils;

import javax.validation.constraints.NotNull;
import java.util.Comparator;
import java.util.Date;

/**
 * 首页
 * Created by defei on 2/5/17.
 */
@Data
@SuppressWarnings({ "rawtypes", "unchecked" })
@Ro(key = "soa:cover:coverRo")
@RoSortedSet(key="list",score="createTimestamp")
public class CoverRo extends BaseRedisObject<String> implements Comparable<CoverRo> {

    /**
     * 名称
     */
    private String name;

    /**
     * 描述
     */
    private String description;

    /**
     * 排序，index大的优先显示
     */
    private Integer index = 0;

    /**
     * 状态
     */
    private CommonStatus status = CommonStatus.DISABLE;

    /**
     * 模板url(优先于模板)
     */
    private String url;

    /**
     * 模板(与模板url互斥)
     */
    private String template;

    /**
     * 模板 用户标识
     * @see com.biz.primus.homepage.enums.CoverClientType
     */
    private Integer coverClientType;

    @NotNull
    private Date startTime;

    @NotNull
    private Date endTime;

    /**
     * 是否在有效时间内
     */
    @JsonIgnore
    public boolean atTheRightTime() {

        long currentTimeMillis = System.currentTimeMillis();
        return startTime != null && endTime != null && currentTimeMillis >= startTime.getTime() && System.currentTimeMillis() <= endTime.getTime();
    }

    @Override
    public int compareTo(CoverRo o) {

        if (o == null){
            return 1;
        } else {
			Comparator nullLowComparator = ComparatorUtils.nullLowComparator(null);
            int indexCompareResult = nullLowComparator.compare(o.getIndex(), getIndex());
            if(indexCompareResult != 0){
                return indexCompareResult;
            }
            int compare = nullLowComparator.compare(getId(), o.getId());
            return compare == 0 ? nullLowComparator.compare(o.getName(), getName()) : compare;
        }
    }
}
